package com.jian.mall.controller;

import com.jian.mall.common.redis.User;
import com.jian.mall.common.utils.R;
import com.jian.mall.interceptor.CartInterceptor;
import com.jian.mall.sevice.CartService;
import com.jian.mall.vo.AddToCartInfo;
import com.jian.mall.vo.CartItem;
import com.jian.mall.vo.CartVo;
import com.jian.mall.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisKeyValueTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 用户的id为redis的key 存储每一个用户的购物车信息
     * 购物车的value使用hash的key-value对存储 skuId为key 其他信息为值
     *
     * @return .
     */
    @GetMapping("/info")
    public R getCartItems() {
        User info = CartInterceptor.userInfoThreadLocal.get();
        CartVo cartVo = cartService.getAllCartInfo(info);
        return R.ok().put("cartVo", cartVo);
    }

    @GetMapping("/selective")
    public R getSelective(){
        List<CartItem> cartItems = cartService.getSelective();

        return R.ok().put("items",cartItems);
    }

    @PostMapping("/addToCart")
    public R addToCart(@RequestBody AddToCartInfo cartInfo) throws ExecutionException, InterruptedException {
        cartService.addToCart(cartInfo);
        return R.ok();
    }

    @PostMapping("/cleanCart")
    public R cleanCart(){
        cartService.cleanCart();
        return R.ok();
    }
    @PostMapping("/updateCartItem")
    public R updateCartItem(@RequestBody CartItem cartItem){
        cartService.updateCartItem(cartItem);
        return R.ok();
    }
    @DeleteMapping("/delete/{cartId}")
    public R deleteByCartSkuId(@PathVariable("cartId")Long cartId){
        cartService.deleteByCartSkuId(cartId);
        return R.ok();
    }
    @DeleteMapping("/delete")
    public R deleteByCartSkuIds(@RequestBody List<Long> skuIds){
        cartService.deleteByCartSkuIds(skuIds);
        return R.ok();
    }
}
